package com.mikasa.springboot.example.redis;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * Created by sherlock on 17/1/12.
 */

@Configuration
@PropertySource(value = "classpath:/jedis.properties")
public class JedisConfig {

    @Value("${jredis.master.ip}")
    private String host;
    @Value("${jredis.master.port}")
    private int port;
    @Value("${jredis.master.password}")
    private String password;
    @Value("${jredis.master.timeout}")
    private int timeout;
    @Value("${jredis.pool.maxTotal}")
    private int maxTotal;
    @Value("${jredis.pool.maxIdle}")
    private int maxIdle;
    @Value("${jredis.pool.maxWaitMillis}")
    private int maxWaitMillis;

    @Bean
    public JedisPoolConfig jedisPoolConfig(){
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(maxTotal);
        config.setMaxIdle(maxIdle);
        config.setMaxWaitMillis(maxWaitMillis);
        return config;
    }

    @Bean
    public JedisPool jedisPool(){
        JedisPoolConfig config = jedisPoolConfig();
        //JedisPool pool = new JedisPool(config,host,port,timeout,password);
        JedisPool pool = new JedisPool(config,host,port,timeout);
        return pool;
    }
}
